/**
 * GossipBasedProtocol.java
 * ************************** 
 * @date Mar 19, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.gossipThread;


/**
 * Description: 
 * Class that gathers an ActiveGossipThread and a PassiveGossipThread
 * in order to provide a complete prototype for a gossip-based 
 * algorithm. 
 * If one want to build a gossip algorithm from scratch need 
 * to implement two subclasses for the active and passive part of the
 * protocol. This can be accomplished by build two classes that
 * inherit from ActiveGossipThread and PassiveGossipThread.
 * Then one can use the actual one: GossipBasedProtocol as a 
 * launcher for the just-created  classes. 
 *
 */
public class GossipBasedProtocol extends GossipThread{

	protected ActiveGossipThreadInterface activeThread;
	protected PassiveGossipThreadInterface passiveThread;
	
	public GossipBasedProtocol(){
		
	}

	/**
	 * Build a GossipBasedProtocol from two classes performing the
	 * Active and Passive actions of a gossip protocol.
	 * @param activeThread
	 * @param passiveThread
	 */
	public GossipBasedProtocol(ActiveGossipThreadInterface activeThread, 
			PassiveGossipThreadInterface passiveThread){
		this.activeThread = activeThread;
		this.passiveThread = passiveThread;
	}
	
	/**
	 * Stop the actual gossip protocol. 
	 */
	public void stopGossipProtocol(){
		this.hasToStop = true;
		activeThread.stopGossipThread();
		passiveThread.stopGossipThread();
	}
	
	/**
	 * Start the actual gossip protocol.
	 */
	public void start(){
		try{
			activeThread.start();
			passiveThread.start();			
		}catch(NullPointerException e){
			e.printStackTrace();
			System.err.println("active thread and passive thread must be initialized!");
		}
	}
		
}
